package edu.ipfw.cs590.cheat.services.utils;
/*
 * Course:    CS 590 Web Services
 * Semseter:  Spring 2009
 * Professor: Sedlemeyer
 * Project:   CHEAT (Code Help Educational Assignment Tool)
 * Due Date:  May 1, 2009
 * Team:      Sarcasm
 *            (Baker, Jason; Mitchell, Robin; Shie, Keith)
 */

import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.web_cat._1_0.assignment_sharing.Assignment;

/**
 *
 * @author rrm46582
 */
public class AssignmentMarshaller
{
    private String assignmentString = null;

    public AssignmentMarshaller(Assignment a)
    {
        try
        {
            StringWriter w = new StringWriter();
            final JAXBContext jaxbContext = JAXBContext.newInstance("org.web_cat._1_0.assignment_sharing");
            final Marshaller marshal = jaxbContext.createMarshaller();
            marshal.marshal(a, w);
            assignmentString = w.toString();
        }
        catch (JAXBException ex)
        {
            Logger.getLogger(AssignmentMarshaller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public AssignmentMarshaller(Document d)
    {
        try
        {
            StringWriter w = new StringWriter();
            DOMSource domSource = new DOMSource(d);
            StreamResult sResult = new StreamResult( w );
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(domSource, sResult);
            assignmentString = w.toString();
        }
        catch (TransformerException ex)
        {
            Logger.getLogger(AssignmentMarshaller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }




    public String getAssignmentString()
    {
        return assignmentString;
    }

}
